import * as network from '@/utils/request';
import $utils from '@/utils/utils';
import { user } from '../../config/Apis';

type Region = {
    label: string;
    value: string;
    children: Region[];
    disabled?: boolean;
}

export async function login(params: any): Promise<any> {
    return network.post(user.login, {
        data: params,
        requestType: 'form'
    });
}

export async function selectLoginStation(params: any): Promise<any> {
    return network.post(user.selectLoginStation, {
        data: params,
        requestType: 'form'
    });
}

export async function resetPassword(params: any): Promise<any> {
    return network.post(user.resetPassword, {
        data: params,
        requestType: 'form'
    });
}

export async function getPcode(params: any): Promise<any> {

    return network.post(user.getPcode, {
        data: params,
        requestType: 'form'
    });
}

export async function getOrderView(params: any): Promise<any> {
    return network.post(user.orderOverview, {
        data: params,
        requestType: 'form'
    });
}

export async function getInComeView(params: any): Promise<any> {
    return network.post(user.incomeOverview, {
        data: params,
        requestType: 'form'
    });
}

export async function getNoticeList(params: any): Promise<any> {
    return network.post(user.noticeList, {
        data: params,
        requestType: 'form'
    });
}

export async function getNoticeDetail(params: any): Promise<any> {
    return network.post(user.noticeInfo, {
        data: params,
        requestType: 'form'
    });
}

export async function getUnReadNum(params: any): Promise<any> {
    return network.post(user.unreadNum, {
        data: params,
        requestType: 'form'
    });
}

export async function readNotice(params: any): Promise<any> {
    return network.post(user.readNotice, {
        data: params,
        requestType: 'form'
    });
}

export async function getRegionList(params: any): Promise<any> {
    return network.post(user.regionList, {
        data: params,
        requestType: 'form'
    });
}

export async function getRegionQuery(params: any): Promise<any> {
    return network.post(user.regionQuery, {
        data: params,
        requestType: 'form',
    });
}

export async function getUsersList(params: any): Promise<any> {
    return network.post(user.userList, {
        data: params,
        requestType: 'form',
    });
}

export async function getUsersDetail(params: any): Promise<any> {
    return network.post(user.userDetail, {
        data: params,
        requestType: 'form',
    });

}

export async function addUser(params: any): Promise<any> {
    return network.post(user.userAdd, {
        data: params,
        requestType: 'form',
    });
}

export async function updateUser(params: any): Promise<any> {
    return network.post(user.userUpdate, {
        data: params,
        requestType: 'form',
    });
}

export async function getRegionData(user?: any) {

    if (!user) { user = $utils.getStorage('user_info'); }
    const { regionCodeList, isAdmin, isRoot } = user;
    // 判断本地是否获取过地址编码列表
    let regionList = $utils.getStorage('region_list');
    if (!regionList || regionList.length === 0) {
        try {
            const msg = await getRegionList({});
            if (msg.code === 0) {
                const data: Region[] = [], list = msg.body;
                for (let i = 0; i < list.length; i++) {
                    const i_index = regionCodeList.findIndex((it: any) => it === list[i].code);
                    const only_index = regionCodeList.findIndex((it: any) => it.substring(0, 2) === list[i].code);
                    const is_province = isAdmin === 1 || isRoot === 1 || i_index > -1, only_show = only_index > -1;
                    if (is_province || only_show) {
                        data.push({
                            label: list[i].name,
                            value: list[i].code,
                            children: [],
                        });
                    }
                    if (list[i].children.length > 0) {
                        for (let j = 0, list_i = list[i].children; j < list_i.length; j++) {
                            const j_index = regionCodeList.findIndex((it: any) => it === list_i[j].code);
                            const j_only_index = regionCodeList.findIndex((it: any) => it.substring(0, 4) === list_i[j].code);
                            const is_city = is_province || j_index > -1, j_only_show = j_only_index > -1;
                            if (is_city || j_only_show) {
                                data[data.length - 1].children?.push({
                                    label: list_i[j].name,
                                    value: list_i[j].code,
                                    children: [],
                                });
                            }
                            if (list_i[j].children.length > 0) {
                                for (let k = 0, list_j = list_i[j].children; k < list_j.length; k++) {
                                    const k_index = regionCodeList.findIndex((it: any) => it === list_j[k].code);
                                    const is_area = is_city || k_index > -1;
                                    if (is_area) {
                                        data[data.length - 1].children[data[data.length - 1].children.length - 1].children?.push({
                                            label: list_j[k].name,
                                            value: list_j[k].code,
                                            children: [],
                                        });
                                    }
                                }
                            }
                        }
                    }
                }
                $utils.setStorage('region_list', data);
                regionList = data;
            }
        }
        catch (e) {
            console.error(e);
        }
    }
    return regionList;
}